#include "armsitem.h"
#include "toolkit.h"
#include "game/gamescene.h"
#include <QDebug>
#include <QPainter>
#include "resources.h"

const float endy = 20;
const float endbeginy = 100;
const float increment = 50;
const float Opacityinc = 0.5;

ArmsItem::ArmsItem(MANTYPE armtp):
    ManItem(armtp),
    m_iFlyNum(-1),
    m_bHalf(true),
    m_endY(endbeginy),
    m_bShowBullet(false),
    m_fBulletOpacity(1.0)
{
    m_FloatSpeed = 0.5;

    switch(m_manTp)
    {
    case CUP_MTP:
    {
        m_animamap[GO_TP] = 1;
        m_animamap[CATCH_TP] = 4;
        m_animamap[TURN_TP] = 6;
        m_animamap[DROP_TP] = 8;
        m_animamap[DROPING_TP] = 10;
        m_animamap[UP_TP] = 13;
        m_animamap[RUN_SQUAT_TP] = 15;
        m_animamap[SOS_FIRE_TP] = 19;
        m_animamap[OPPSQUAT_TP] = 21;

        m_beginY = 135;
        m_leftX = 44;
        m_rightX = 46;
    }
        break;
    case SOLDIER_MTP:
    {
        m_animamap[GO_TP] = 1;
        m_animamap[CATCH_TP] = 4;
        m_animamap[TURN_TP] = 7;
        m_animamap[DROP_TP] = 9;
        m_animamap[DROPING_TP] = 11;
        m_animamap[UP_TP] = 14;
        m_animamap[RUN_SQUAT_TP] = 16;
        m_animamap[SOS_FIRE_TP] = 20;
        m_animamap[OPPSQUAT_TP] = 22;

        m_beginY = 158;
        m_leftX = 22;
        m_rightX = 20;
    }
        break;
    default:
        break;
    }

    SetDetectX();
}

void ArmsItem::logic()
{
    if(m_bRun)
    {
        switch(m_mananimatp)
        {
        case TURN_TP:
        {
            if(m_x == m_animamap[m_mananimatp] * m_ManSize.width())
            {
                SetAnimaType(GO_TP);
            }
            else
            {
                m_x += m_ManSize.width();
            }
        }
            break;
        case GO_TP:
        {
            if(m_x == m_animamap[m_mananimatp] * m_ManSize.width())
            {
                m_x = 0;
            }
            else
            {
                m_x += m_ManSize.width();
            }

            Go();
        }
            break;
        case CATCH_TP:
        {
            if(m_x == m_animamap[m_mananimatp] * m_ManSize.width())
            {
                m_x = (m_animamap[m_mananimatp - 1] + 1) * m_ManSize.width();
            }
            else
            {
                m_x += m_ManSize.width();
            }

            Cover();
        }
            break;
        case DROPING_TP:
        {
            if(m_x == m_animamap[m_mananimatp] * m_ManSize.width())
            {
                m_x = (m_animamap[m_mananimatp - 1] + 1) * m_ManSize.width();
            }
            else
            {
                m_x += m_ManSize.width();
            }

            Droping();
        }
            break;
        case UP_TP:
        {
            if(m_x == m_animamap[m_mananimatp] * m_ManSize.width())
            {
                m_detectX = ToolKit::m_gamescene->getFlyPos(NEAR_TP,m_iFlyNum,x());
                if(IsTurn())
                {
                    Turn();
                }
                else
                {
                    SetAnimaType(GO_TP);
                }
            }
            else
            {
                m_x += m_ManSize.width();
            }
        }
            break;
        case DROP_TP:
        {
            if(m_x == m_animamap[m_mananimatp] * m_ManSize.width())
            {
                SetAnimaType(DROPING_TP);
            }
            else
            {
                m_x += m_ManSize.width();
            }
        }
            break;
        case RUN_SQUAT_TP:
        {
            if(m_x == m_animamap[m_mananimatp] * m_ManSize.width())
            {
                if(ToolKit::m_bVoice && ToolKit::m_bgameVoice)
                {
                    if(m_manTp == CUP_MTP)
                        ToolKit::playSound(11);
                    else
                        ToolKit::playSound(13);
                }

                SetAnimaType(SOS_FIRE_TP);
                initBullet();
            }
            else
            {
                m_x += m_ManSize.width();
            }
        }
            break;
        case SOS_FIRE_TP:
        {
            if(m_x == m_animamap[m_mananimatp] * m_ManSize.width())
            {
                SetAnimaType(NULL_TP);
                QTimer::singleShot(2000,this,SLOT(Fire()));
                emit FireSignal(m_iFlyNum,m_manTp);
            }
            else
            {
                m_x += m_ManSize.width();
            }
        }
            break;
        case OPPSQUAT_TP:
        {
            if(m_x == m_animamap[m_mananimatp] * m_ManSize.width())
            {
                m_detectX = ToolKit::m_gamescene->getFlyPos(NEAR_TP,m_iFlyNum,x());
                if(IsTurn())
                {
                    Turn();
                }
                else
                {
                    SetAnimaType(GO_TP);
                }
            }
            else
            {
                m_x += m_ManSize.width();
            }
        }
            break;
        default:
            break;
        }

        if(m_bdrawlink)
            DrawLink();

        if(m_bShowBullet)
            BulletLogic();

        update();
    }
}

void ArmsItem::SetDetectX()
{
    if(ToolKit::m_gamescene != NULL)
    {
        switch(m_manTp)
        {
        case CUP_MTP:
        {
            m_detectX = ToolKit::m_gamescene->getFlyPos(RANDOM_TP,m_iFlyNum);
        }
        break;
        case SOLDIER_MTP:
        {
            if(m_bleft)
                m_detectX = ToolKit::m_gamescene->getFlyPos(RIGHT_TP,m_iFlyNum);
            else
                m_detectX = ToolKit::m_gamescene->getFlyPos(LEFT_TP,m_iFlyNum);
        }
        break;
        default:
            break;
        }
    }
}

void ArmsItem::Go()
{
    if(m_bleft)
    {
        if(x() <= m_detectX)
        {
            SetAnimaType(RUN_SQUAT_TP);
        }
        else
        {
            setX(x() - 4);
        }
    }
    else
    {
        if(x() >= m_detectX)
        {
            qWarning()<<m_detectX;
            SetAnimaType(RUN_SQUAT_TP);
        }
        else
        {
            setX(x() + 4);
        }
    }
}

bool ArmsItem::IsTurn()
{
    bool bTurn = false;

    if(x() <= m_detectX)
    {
        if(m_bleft)
        {
           bTurn = true;
        }
    }
    else
    {
        if(!m_bleft)
        {
            bTurn = true;
        }
    }

    return bTurn;
}

void ArmsItem::Fire()
{
    if(m_mananimatp == NULL_TP)
    {
        if(ToolKit::m_bVoice && ToolKit::m_bgameVoice)
        {
            if(m_manTp == CUP_MTP)
                ToolKit::playSound(11);
            else
                ToolKit::playSound(13);
        }

        SetAnimaType(SOS_FIRE_TP);
        initBullet();
    }
}

void ArmsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->drawPixmap(QRectF(m_ManPt,m_ManSize),m_animaimage,QRectF(QPointF(m_x,0),m_ManSize));

    if(m_mananimatp == CATCH_TP)
    {
       switch(m_CoverType)
       {
       case COMMON_CTP:
       {
           painter->drawPixmap(QRectF(m_CommonPt,m_CoverCommonSize),m_CoverCommonImage,
                               QRectF(QPointF(m_coverX,0),m_CoverCommonSize));
       }
           break;
       case DIS_CTP:
       {
           painter->drawPixmap(QRectF(m_DisappearPt,m_CoverDisappearSize),m_CoverDisappearImage,
                               QRectF(QPointF(m_coverX,0),m_CoverDisappearSize));
       }
           break;
       case BOMB_CTP:
       {
           painter->drawPixmap(QRectF(m_DisappearPt,m_CoverBombSize),m_CoverBombImage,
                               QRectF(QPointF(m_coverX,0),m_CoverBombSize));
       }
           break;
       default:
           break;
       }
    }

    if(m_bdrawlink)
    {
        painter->save();
        painter->setOpacity(m_linkOpacity);
        painter->drawPixmap(QRectF(20.0,100.0,32.0,36.0),ToolKit::m_rc->m_LinkImage,QRectF(0.0,0.0,32.0,36.0));
        painter->restore();
    }

    if(m_bShowBullet)
    {
        painter->save();

        painter->setOpacity(m_fBulletOpacity);
        painter->setPen(QPen(QBrush(Qt::white),(m_manTp == CUP_MTP) ? 1 : 3));
        if(m_bleft)
            painter->drawLine(QPointF(m_leftX,m_beginY),QPointF(m_leftX,m_endY));
        else
            painter->drawLine(QPointF(m_rightX,m_beginY),QPointF(m_rightX,m_endY));

        painter->restore();
    }

//    painter->drawRect(boundingRect());
}

void ArmsItem::BulletLogic()
{
    if(m_bShowBullet)
    {
        if(m_endY >= endy)
        {
            m_endY -= increment;
        }
        else
        {
            if(m_fBulletOpacity >= 0)
            {
                m_fBulletOpacity -= Opacityinc;
            }
            else
            {
                m_bShowBullet = false;
            }
        }
    }
}

void ArmsItem::initBullet()
{
    m_bShowBullet = true;
    m_fBulletOpacity = 1.0;
    m_endY = endbeginy;
}
